package com.ccb.sc.live.controller;

import com.ccb.sc.auth.TokenAuth;
import com.ccb.sc.auth.TokenModel;
import com.ccb.sc.live.controller.query.LiveStreamingGoodsQueryParam;
import com.ccb.sc.live.service.LiveStreamingService;
import com.ccb.sc.live.vo.LiveStreamingGoodsGroupListVo;
import com.ccb.sc.live.vo.LiveStreamingListVo;
import com.ccb.sc.live.vo.LiveStreamingPreviewVo;
import com.ccb.sc.live.vo.LiveStreamingReserveListVo;
import com.ccb.sc.common.bind.annotation.CurrentUser;
import com.ccb.sc.common.bind.annotation.Limit;
import com.ccb.sc.common.bind.page.PageParams;
import com.ccb.sc.common.bind.page.PageVo;
import com.ccb.sc.common.bind.response.ResBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author ken
 * @version 1.0
 * @date 2020-09-21
 */
@RestController
@Api(value = "直播管理", tags = "直播管理")
@RequestMapping("/app/live-streaming")
public class LiveStreamingController {

    @Autowired
    private LiveStreamingService liveStreamingService;

    @Limit(period = 5, count = 5)
    @GetMapping("/list")
    @ApiOperation(value = "直播列表", response = LiveStreamingListVo.class, responseContainer = "list")
    public ResBody<?> list(@ApiIgnore @CurrentUser TokenModel token) {
        Long memberId = token != null ? token.getId() : null;
        List<LiveStreamingListVo> result = this.liveStreamingService.selectMonthLiveStreamingList(memberId);
        return ResBody.success(result);
    }

    @Limit(period = 5, count = 5)
    @GetMapping("/preview")
    @ApiOperation(value = "直播预告", response = LiveStreamingPreviewVo.class)
    public ResBody<?> preview(@ApiIgnore @CurrentUser TokenModel token) {
        Long memberId = token != null ? token.getId() : null;
        LiveStreamingPreviewVo preview = this.liveStreamingService.preview(memberId);
        return ResBody.success(preview);
    }

    @Limit(period = 10, count = 10)
    @GetMapping("/goods")
    @ApiOperation(value = "直播商品", response = LiveStreamingGoodsGroupListVo.class, responseContainer = "list")
    public ResBody<?> goods(@ApiIgnore @CurrentUser TokenModel token, LiveStreamingGoodsQueryParam queryParam) {
        Long memberId = token != null ? token.getId() : null;
        List<LiveStreamingGoodsGroupListVo> liveStreamingGoodsList = this.liveStreamingService.findLiveStreamingGoodsList(memberId, queryParam);
        return ResBody.success(liveStreamingGoodsList);
    }

    @TokenAuth
    @PostMapping("/reserve")
    @ApiOperation(value = "直播预约")
    public ResBody<?> reserve(@ApiIgnore  @CurrentUser TokenModel token, @RequestParam Long liveId, Long liveStreamingGoodsId) {
        Assert.isTrue(liveId !=null || liveStreamingGoodsId !=null, "直播id不能为空");
        this.liveStreamingService.reserve(token.getId(), liveId, liveStreamingGoodsId);
        return ResBody.success();
    }

    @TokenAuth
    @GetMapping("/reserve/list")
    @ApiOperation(value = "直播预约列表", response = LiveStreamingReserveListVo.class, responseContainer = "list")
    public ResBody<?> reserveList(@ApiIgnore  @CurrentUser TokenModel token, PageParams queryParam) {
        PageVo<LiveStreamingReserveListVo> page = this.liveStreamingService.reserveList(token.getId(), queryParam);
        return ResBody.success(page);
    }


}
